//
//  ICarouselView.m
//  BaiYing_Thinker
//
//  Created by 鹏 朱 on 15/11/12.
//  Copyright (c) 2015年 XKJH. All rights reserved.
//

#import "ICarouselView.h"
#import "ChatViewController.h"
#import "TouchTableView.h"
#import "AppDelegate.h"
#import "XKO_CreateUIViewHelper.h"
#import "MainChatController.h"

@interface ICarouselView()

@end

@implementation ICarouselView

- (void)dealloc
{
    self.carousel.delegate = nil;
    self.carousel.dataSource = nil;
    self.carousel = nil;
}

- (id)initWithFrame:(CGRect)frame ds:(NSArray *)ds{
    
    if (self = [super initWithFrame:frame]) {
        
        _ds = ds;
        self.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:self.carousel];

    }
    return self;
}

- (iCarousel *)carousel {
    
    if (!_carousel) {
        _carousel = [[iCarousel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _carousel.type = iCarouselTypeLinear;
        _carousel.dataSource = self;
        _carousel.delegate = self;
        _carousel.vertical = NO;
        _carousel.bounceDistance = 0.3;
    }
    
    return _carousel;
}

#pragma mark -
#pragma mark iCarousel methods

- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
    
    if (carousel.vertical) {

        return kAppDelegate.subjectList.count;
        
    } else {
        
        if (self.ds && self.ds.count > 0) {
            return self.ds.count;
        } else {
            return 1;
        }
    }
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view {
    
    if (self.ds && self.ds.count > 0) {

        NSMutableArray *previousList = nil;
        NSMutableArray *nextList = nil;
        NSMutableArray *list = [self.ds objectAtIndex:index];
        NSInteger nextIndex = index;
        NSInteger previousIndex = index;

        if (self.previousDataList) {
            if (previousIndex >= self.previousDataList.count) {
                previousIndex = self.previousDataList.count - 1;
            }
            previousList = [self.previousDataList objectAtIndex:previousIndex];
        }
        
        if (self.nextDataList) {
            if (nextIndex >= self.nextDataList.count) {
                nextIndex = self.nextDataList.count - 1;
            }

            nextList = [self.nextDataList objectAtIndex:nextIndex];
        }

        ChatViewController *iv;
        if (view) {
            
            iv = (ChatViewController *)view;
            iv.dataListOne = list;
            
            if (previousList) {
                iv.previousDataList = previousList;
            }
            if (nextList) {
                iv.nextDataList = nextList;
            }
            
            [iv reloadData];
            
        } else {
            
            iv =[[ChatViewController alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) dataSource:list previousDataList:previousList nextDataList:nextList];
            iv.scrollEnabled = YES;
            iv.scrollsToTop = NO;
            iv.showsVerticalScrollIndicator = NO;
            
            view = iv;
        }
        
    } else {
        
        ChatViewController *chatViewController = [[ChatViewController alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) dataSource:nil previousDataList:nil nextDataList:nil];
        [chatViewController reloadData];
        
        return chatViewController;
    }
    
    return view;
}

- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value {
    switch (option)
    {
        case iCarouselOptionSpacing:
        {
            return value * 1.05f;
        }
        default:
        {
            return value;
        }
    }
}

#pragma mark - callback
#pragma mark - iCarouselDelegate

- (void)carousel:(__unused iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"Tapped view number: %ld", index);
    if (carousel.vertical) {
        
        self.carousel.vertical = NO;
        [self.carousel reloadData];
        
        if (_subjectDidSelectedBlock) {
            _subjectDidSelectedBlock(index);
        }
    }
}

- (void)carouselCurrentItemIndexDidChange:(__unused iCarousel *)carousel
{
    NSLog(@"Index: %@", @(carousel.currentItemIndex));
    
//    [carousel reloadItemAtIndex:carousel.currentItemIndex animated:NO];

}

@end
